package com.kt.controller;

import com.kt.dao.imp.UserDAOImp;
import com.kt.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/adminLogin.do")
public class AdminLoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("username");
        String password = req.getParameter("password");
        UserDAOImp userDAOImp = new UserDAOImp();
        User user = userDAOImp.findByUsername(userName);
        req.setCharacterEncoding("utf-8");
        if(user != null) {
            if (password.equals(user.getPassword()) && user.getLevel() == 3){
                resp.getWriter().write("ok");
                req.setAttribute("username", userName);
                return;
            }
        }

        resp.getWriter().write("error");
    }
}
